package com.chixing.controller;


import com.chixing.entity.DTO.RoomChangeDTO;
import com.chixing.service.IOrderChangeService;
import com.chixing.service.IRoomsService;
import com.chixing.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.time.LocalDate;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author smith
 * @since 2025-09-30
 */
@CrossOrigin
@RestController
@RequestMapping("/rooms")
public class RoomsController {

    @Autowired
    private IRoomsService roomsService;
    @Autowired
    private IOrderChangeService orderChangeService;

    @GetMapping("hotelId/{hotelId}")
    public Result getHotelRooms(@PathVariable("hotelId") Integer hotelId){
        return roomsService.getHotelRooms(hotelId);
    }

    /**
     * 获取可用调换房型
     */
    @GetMapping("/available-room-types")
    public Result getAvailableRoomTypes(
            @RequestParam Long hotelId,
            @RequestParam Long currentRoomTypeId,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate checkIn,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate checkOut) {
        return orderChangeService.getAvailableRoomTypes(hotelId, currentRoomTypeId, checkIn, checkOut);
    }

    /**
     * 执行房型调换
     */
    @PostMapping("/change-type")
    public Result changeRoomType(@RequestBody RoomChangeDTO dto) {
        return orderChangeService.changeRoomType(
                dto.getOrderId(),
                dto.getOldRoomTypeId(),
                dto.getNewRoomTypeId()
        );
    }

}
